package org.example.controller;

import org.example.data.Account;
import org.example.data.Result;
import org.example.view.Index;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class IndexController {
    public Result<List<Account>> getCheck(Integer id){
        try {
            return new Result<>(true,"查询成功",selectAccount(id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public Result<Integer> insert(Account account,Integer id) {
        try {
            return new Result<>(true,"添加成功",insertToAccount(account,id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public Result<List<Account>> query(String startDate, String endDate) {
        try {
            return new Result<>(true,"查询成功",querytAccount(startDate,endDate));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public Result<List<Account>> query(String startDate, String endDate,String type) {
        try {
            return new Result<>(true,"查询成功",querytAccount(type,startDate,endDate));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private List<Account> querytAccount(String startDate, String endDate) throws SQLException {
        String sql = "select * from account where create_time > ? and create_time < ?;";
        MySQL mySQL = new MySQL();
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1,startDate);
        statement.setString(2,endDate);
        ResultSet resultSet = statement.executeQuery();
        List<Account> list = new ArrayList<>();
        while (resultSet.next()){
            Integer id = resultSet.getInt("id");
            String createTime = resultSet.getString("create_time");
            String type = resultSet.getString("type");
            String content  = resultSet.getString("content");
            Double amount = resultSet.getDouble( "amount");
            Account account = new Account(id,createTime,type,content,amount);
            list.add(account);
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return list;
    }
    private List<Account> querytAccount(String type,String startDate, String endDate) throws SQLException {
        String sql = "select * from account where create_time > ? and create_time < ? and type = ?;";
        MySQL mySQL = new MySQL();
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1,startDate);
        statement.setString(2,endDate);
        statement.setString(3,type);
        ResultSet resultSet = statement.executeQuery();
        List<Account> list = new ArrayList<>();
        while (resultSet.next()){
            Integer id = resultSet.getInt("id");
            String createTime = resultSet.getString("create_time");
            String type1 = resultSet.getString("type");
            String content  = resultSet.getString("content");
            Double amount = resultSet.getDouble( "amount");
            Account account = new Account(id,createTime,type1,content,amount);
            list.add(account);
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return list;
    }

    private List<Account> selectAccount(Integer userId) throws SQLException {
        String sql = "select * from account where userid = ?;";
        MySQL mySQL = new MySQL();
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setInt(1,userId);
        ResultSet resultSet = statement.executeQuery();
        List<Account> list = new ArrayList<>();
        while (resultSet.next()){
            Integer id = resultSet.getInt("id");
            String createTime = resultSet.getString("create_time");
            String type = resultSet.getString("type");
            String content  = resultSet.getString("content");
            Double amount = resultSet.getDouble( "amount");
            Account account = new Account(id,createTime,type,content,amount);
            list.add(account);
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return list;
    }
    private Integer insertToAccount(Account account,Integer id) throws SQLException {
        String sql = "insert into account (create_time,type,content,amount,userid) values (?,?,?,?,?)";
        MySQL mySQL = new MySQL();
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, account.getDate());
        statement.setString(2, account.getType());
        statement.setString(3, account.getContent());
        statement.setDouble(4, account.getAmount());
        statement.setInt(5, id);

        int n = statement.executeUpdate(); // 执行插入操作
        statement.close();
        mySQL.close();

        return n;
    }



}
